package com.GlobalPaint.app.ui.Login;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.LogBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.bean.codeBean;
import com.GlobalPaint.app.utils.CommUtility;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.blankj.utilcode.utils.RegexUtils;
import com.blankj.utilcode.utils.SPUtils;
import com.blankj.utilcode.utils.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.GlobalPaint.app.R.id.ed_possword;

public class forgetActivity extends AppCompatActivity {

    @BindView(R.id.iv_goback)
    ImageView ivGoback;
    @BindView(R.id.tv_common_center)
    TextView tvCommonCenter;
    @BindView(R.id.tv_fresh)
    TextView tvFresh;
    @BindView(R.id.ed_phone)
    EditText edPhone;
    @BindView(R.id.ed_yzm)
    EditText edYzm;
    @BindView(R.id.tv_getcode)
    TextView tvGetcode;
    @BindView(ed_possword)
    EditText edPossword;
    @BindView(R.id.bt_submit)
    Button btSubmit;
    private codeBean codebean;
    private Pattern pattern;
    private boolean matches;
    private SPUtils spUtils;
    private LogBean logBean;
    private final int CMD_GET_CODE = 0x0001;
    private final int CMD_GET_VALIDATE = 0x0002;
    private Handler mhandler = new Handler() {
        public void handleMessage(Message message) {
            switch (message.what) {
                case CMD_GET_CODE:
                    String url = Constants.JASON_SERVICE_URL + "user/sendSms?type=2&phone=" + String.valueOf(edPhone.getText());
                    DataManager.getInst().getHttpRequestJsonClass(url, "dd", codeBean.class, new DataManager.Callback() {
                        @Override
                        public void onSuccess(Object o) {
                            codebean = (codeBean) o;
                            if (codebean.getStatus() == 1) {
                                runnable.run();
                            } else {
                                Toast.makeText(forgetActivity.this, codebean.getMsg(), Toast.LENGTH_LONG).show();
                            }

                        }

                        @Override
                        public void onFailure(ReturnStatusResultEntity error) {
                            Toast.makeText(forgetActivity.this, "获取验证码失败请稍后再试", Toast.LENGTH_LONG).show();
                        }
                    });

                    break;
                case CMD_GET_VALIDATE:
                    String url1 = Constants.JASON_SERVICE_URL + "user/resetPassword?userName="
                            + String.valueOf(edPhone.getText().toString()) + "&newPwd="
                            + String.valueOf(edPossword.getText()) + "&verificationCode=" + String.valueOf(edYzm.getText());
                    DataManager.getInst().getHttpRequestJsonClass(url1, "dd", LogBean.class, new DataManager.Callback() {
                        @Override
                        public void onSuccess(Object o) {
                            logBean = (LogBean) o;
                            if (logBean.getStatus() == 1) {
                               /* userEntity = logBean.getData();
                                spUtils.putString("user", new Gson().toJson(logBean.getData()));
                                spUtils.putString("mobile", String.valueOf(edPhone.getText().toString()));
                                loginFailed = false;*/
                                Intent intent = new Intent(getApplicationContext(), Login.class);
                                startActivity(intent);
                                finish();
                            } else {
                                Toast.makeText(forgetActivity.this, logBean.getMsg(), Toast.LENGTH_LONG).show();
                            }

                        }

                        @Override
                        public void onFailure(ReturnStatusResultEntity error) {
                            //Toast.makeText(Register.this,"获取验证码失败请稍后再试",Toast.LENGTH_LONG).show();
                        }
                    });
                    break;
            }
        }
    };
    int recLen = 0;
    int total = 60;
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            recLen++;
            int remain = total - recLen;
            if (remain == 0) {
                recLen = 0;
                tvGetcode.setText("重新发送");
                tvGetcode.setEnabled(true);
                return;
            } else {
                tvGetcode.setEnabled(false);
                String display = String.valueOf(remain) + "秒后重发";
                tvGetcode.setText(display);
            }
            handler.postDelayed(this, 1000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        ButterKnife.bind(this);
        spUtils = new SPUtils(DataManager.context, "login");
    }

    private void getSubmit() {
        //检查
        String phone = String.valueOf(edPhone.getText());
        if (TextUtils.isEmpty(phone)) {
            CommUtility.ShowMsgShort(this, "请输入手机号");
            return;
        } else {
            if (!RegexUtils.isMobileSimple(phone)) {
                CommUtility.ShowMsgShort(this, "请输入正确手机号");
                return;
            }

            String validateCode = String.valueOf(edYzm.getText());
            if (TextUtils.isEmpty(validateCode)) {
                CommUtility.ShowMsgShort(this, "请获取验证码");
                return;
            } else if (validateCode.length() == 6) {
                Pattern pattern = Pattern.compile("[0-9]*");
                Matcher isNum = pattern.matcher(validateCode);
                if (!isNum.matches()) {
                    CommUtility.ShowMsgShort(this, "验证码为6位数字");
                    return;
                }
            } else {
                CommUtility.ShowMsgShort(this, "验证码为6位数字");
                return;
            }
            String password = String.valueOf(edPossword.getText());
            if (TextUtils.isEmpty(password)) {
                CommUtility.ShowMsgShort(this, "请输入新密码");
                return;
            } else {
                if (password.length() < 6 || password.length() > 20) {
                    CommUtility.ShowMsgShort(this, "请输入6-20位密码");
                    return;
                }
            }
            mhandler.sendEmptyMessage(CMD_GET_VALIDATE);

        }
    }

    @OnClick({R.id.iv_goback, R.id.tv_getcode,R.id.bt_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_goback:
                finish();
                break;
            case R.id.tv_getcode:
                String phone = String.valueOf(edPhone.getText());
                String regexPhone = "1[34578]\\d{9}";
                pattern = Pattern.compile(regexPhone);
                matches = pattern.matcher(phone).matches();
                if (StringUtils.isEmpty(phone)) {
                    CommUtility.ShowMsgShort(this, "手机号码不能为空");
                    return;
                }
                if (!matches) {
                    CommUtility.ShowMsgShort(this, "您输入的手机号码有误");
                    return;
                }
                if (!RegexUtils.isMobileSimple(phone)) {
                    CommUtility.ShowMsgShort(this, "请填写手机号");
                    return;
                }
                mhandler.sendEmptyMessage(CMD_GET_CODE);
                break;
            case R.id.bt_submit:
                getSubmit();
                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
